package com.mohe.zhihui.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.mohe.zhihui.model.Activity;
import com.mohe.zhihui.model.Notice;
import com.mohe.zhihui.service.interfaces.IActivity;
import com.mohe.zhihui.service.interfaces.INotice;
import com.mohe.zhihui.utils.Json;
import com.mohe.zhihui.utils.JsonUtil;
import com.mohe.zhihui.utils.Page;
import com.opensymphony.xwork2.ActionSupport;

public class LatestNoticeAction extends ActionSupport {

	private static final long serialVersionUID = 2780426917303636915L;
	
	//	ApplicationContext ac;

	private INotice noticeServ;
	
	private List<Notice> noticeList;
	private Notice notice = new Notice();
	private Page page = new Page();

	public INotice getNoticeServ() {
		return noticeServ;
	}

	public void setNoticeServ(INotice noticeServ) {
		this.noticeServ = noticeServ;
	}

	public List<Notice> getNoticeList() {
		return noticeList;
	}

	public void setNoticeList(List<Notice> noticeList) {
		this.noticeList = noticeList;
	}

	public Notice getNotice() {
		return notice;
	}

	public void setNotice(Notice notice) {
		this.notice = notice;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
	
	public String getNoticeListForPage() {
		int count = noticeServ.getCount(notice);
		page.setPageSize(8);
		page.setCount(count);
		noticeList = noticeServ.getNoticeList(notice, page);
		return "notice.jsp";
	}
	
	public void searchNoticeList() {
		Json json = new Json();
		List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
		int count = noticeServ.getCount(notice);
		page.setPageSize(8);
		page.setCount(count);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Notice> list = noticeServ.getNoticeList(notice, page);
		if (list != null) {
			int index = (page.getCurrPage() - 1) * page.getPageSize() + 1;
			for (Notice s : list) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("index", index + "");
				if (s.getTimeKey() != null) {
					map.put("timeKey", s.getTimeKey() + "");
				} else {
					map.put("timeKey", "0");
				}
				if (!StringUtils.isBlank(s.getTitle())) {
					map.put("title", s.getTitle());
				} else {
					map.put("title", "");
				}
				if (!StringUtils.isBlank(s.getContent())) {
					map.put("content", s.getContent());
				} else {
					map.put("content", "");
				}
				if (s.getCreateDate() != null) {
					map.put("date", df.format(s.getCreateDate()));
				} else {
					map.put("date", "");
				}
				mapList.add(map);
				index++;
			}
		}
		json.setObject(page);
		json.setMap(mapList);
		JsonUtil.writeJson(json);
	}

}
